package com.fiidee.blog.service.impl;

import com.fiidee.blog.dto.ConfigDTO;
import com.fiidee.blog.entity.Config;
import com.fiidee.blog.mapper.ConfigMapper;
import com.fiidee.blog.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 瑱彬 on 2015/5/27.
 */

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    ConfigMapper configMapper;

    private static Map<String, String> configTmpMap = new HashMap<>();

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return configMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Config record) {
        return configMapper.insert(record);
    }

    @Override
    public int insertSelective(Config record) {
        return configMapper.insertSelective(record);
    }

    @Override
    public Config selectByPrimaryKey(Integer id) {
        return configMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Config record) {
        return configMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Config record) {
        return configMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Config> selectByDTO(ConfigDTO dto) {
        return configMapper.selectByDTO(dto);
    }

    @Override
    public int deleteInIds(String ids) {
        return configMapper.deleteInIds(ids);
    }

    @Override
    public String getConfig(String key) {
        String configTmp = configTmpMap.get(key);
        if (null == configTmp || "".equals(configTmp)) {
            String config = configMapper.getConfig(key);
            if ("pageRows".equals(key)) {
                if (null == config || "".equals(config)) {
                    return "10";
                }
                try {
                    Integer.valueOf(config);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    return "10";
                }
            } else if ("contentDateFormat".equals(key)) {
                if (null == config || "".equals(config)) {
                    return "yyyy/MM/dd";
                }
            } else if ("summaryLength".equals(key)) {
                if (null == config || "".equals(config)) {
                    return "50";
                }
                try {
                    Integer.valueOf(config);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    return "50";
                }

            }
            configTmpMap.put(key,config);
            configTmp = config;
        }
        return configTmp;
    }

    @Override
    public int getNum(String key) {
        String pageRows = this.getConfig(key);
        try {
            int rowsInt = Integer.valueOf(pageRows);
            return rowsInt;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
    }

}
